/*
* 学员信息完善的路由组件
* */
import React, {Component} from 'react'
import {connect} from 'react-redux'
import {Redirect} from 'react-router-dom'
import {NavBar, InputItem, TextareaItem, Button} from 'antd-mobile'
import HeaderSelector from '../../components/header-selector/header-selector'

import {updateUser} from '../../redux/actions'


class XueyuanInfo extends Component{



    state = {
        header: '',   // 头像名称
        belongClass: '',  // 所属班级
        info: '',   // 个人简介
        school: '',  //  毕业院校
        degree: '',    // 学历 : 本科/硕士/博士
    }

    // 更新header状态  (头像)
    setHeader = (header) => {
        console.log('---------')
        console.log(header)
        console.log('---------')
        this.setState({
            header:header
        })
        // console.log(this.state)
    }


    // 更新数据
    handleChange = (name, value) => {
        this.setState({
            [name]: value
        })
    }
    // 发送请求,保存数据
    save = () => {
        // console.log(this.state)
        this.props.updateUser(this.state)
    }







    render() {
        /*如果信息已经完善, 自动重定向对应的主界面*/
        // const {type} = this.props.user.data

        const {type, header} = this.props.user

        if(header) { // 说明信息已经完善
            const path = type==='banwei' ? '/banwei' : '/xueyuan'
            return <Redirect to={path}/>
        }
        return (
            <div>
                <NavBar>学员信息完善</NavBar>
                <HeaderSelector setHeader={this.setHeader}/>
                <InputItem placeholder='请输入班级'
                    onChange={val => {this.handleChange('belongClass', val)}}>所属班级:</InputItem>
                <InputItem placeholder='请输入毕业院校'
                    onChange={val => {this.handleChange('school', val)}}>毕业院校:</InputItem>
                <InputItem placeholder='请输入学历'
                    onChange={val => {this.handleChange('degree', val)}}>学历:</InputItem>
                <TextareaItem title='技能描述' rows={3}
                    onChange={val => {this.handleChange('info', val)}}/>
                <Button type='primary' onClick={this.save}>保&nbsp;&nbsp;&nbsp;存</Button>
            </div>
        )
    }
}


export default connect(
    state => ({user: state.user}),
    {updateUser}
)(XueyuanInfo)


